<?php

namespace app\result\extend;

use app\model\extend\Plugins;
use app\model\extend\UserPlugins;
use app\result\BaseResult;

class ExtendResult extends BaseResult
{
    public function list($user_id, $args = [])
    {
        $page = $args['page'] ?? 1;
        $limit = $args['limit'] ?? 10;
        $keyword = $args['keyword'] ?? '';
        $where = [
            // ['status', '=', 1]
        ];
        if ($keyword) {
            $where[] = ['name', 'like', "%{$keyword}%"];
        }
        $where[] = ['pts', 'find in set', BS_FROM];
        $model = new Plugins();
        $total = $model->data_count($where);
        $data = $model->data_list($where, 'plugin_id,name,remark,icon,status', 'plugin_id desc', $page, $limit);
        if ($data) {
            $myPlugins = (new UserPlugins())->data_list_nopage(['user_id' => $user_id], 'plugin_id,sort');
            if ($myPlugins) {
                $myPlugins = array_column($myPlugins, null, 'plugin_id');
            }
            foreach ($data as &$v) {
                $v['added'] = isset($myPlugins[$v['plugin_id']]);
                $v['buttons'] = [
                    'add' => $v['added'] ? 0 : 1,//启用按钮
                    'remove' => $v['added'] ? 1 : 0,//禁用按钮
                    'top' => !empty($myPlugins[$v['plugin_id']]['sort']) ? 0 : 1,//置顶按钮
                ];
            }
            unset($v);
        }
        return self::success_return('success', [
            'list' => $data,
            'total' => $total
        ]);
    }

    public function get($args = [])
    {
        $id = $args['id'] ?? 0;
        if (!$id) {
            return self::error_return('参数错误');
        }
        $data = (new Plugins())->data_get(['plugin_id' => $id]);
        $data['content'] = htmlspecialchars_decode($data['content']);
        $path_list = [
            [
                'title' => '安全中心',
                'path' => '/pages/setting/safe/index',
                'jump_type' => 'inner',//inner=内页，tab=底部导航，link=外链
            ],
            [
                'title' => '绑定邮箱',
                'path' => '/pages/setting/safe/email',
                'jump_type' => 'inner',//inner=内页，tab=底部导航，link=外链
            ],

        ];
        return self::success_return('success', [
            'info' => $data,
            'path_list' => $path_list
        ]);
    }

    public function save($user_id, $args)
    {
        $plugin_id = $args['plugin_id'] ?? 0;
        if (!$plugin_id) {
            return self::error_return('参数错误');
        }
        $action = $args['action'] ?? '';
        if (!$action) {
            return self::error_return('操作类型错误');
        }
        $plugin = (new Plugins())->data_get(['plugin_id' => $plugin_id]);
        if (!$plugin) {
            return self::error_return('插件不存在');
        } elseif ($plugin['status'] != 1) {
            return self::error_return('插件暂未开放');
        }
        if (BS_VERSIONCODE < $plugin['min_version']) {
            return self::error_return('请升级到' . $plugin['min_version'] . '+版本才能使用');
        }
        $model = new UserPlugins();
        $data = $model->data_get([['user_id', '=', $user_id], ['plugin_id', '=', $plugin_id]]);
        if ($action == 'add' && $data) {
            return self::error_return('已添加');
        }
        if ($action == 'top' && !$data) {
            return self::error_return('未添加');
        }
        if ($action == 'add') {
            // return self::error_return('暂未开放');
            $data = [
                'user_id' => $user_id,
                'plugin_id' => $plugin_id,
                'sort' => 0
            ];
            $model->data_add($data);
            return self::success_return('添加成功');
        } else {
            $model->data_update(['id' => $data['id']], ['sort' => $data['sort'] ? 0 : 10]);
            return self::success_return('操作成功');
        }
    }

    public function delete($user_id, $args)
    {
        $plugin_id = $args['plugin_id'] ?? 0;
        if (!$plugin_id) {
            return self::error_return('参数错误');
        }
        $model = new UserPlugins();
        $data = $model->data_get([['user_id', '=', $user_id], ['plugin_id', '=', $plugin_id]]);
        if (!$data) {
            return self::error_return('未添加');
        }
        $model->data_del(['id' => $data['id']]);
        return self::success_return('插件已移除');
    }

    //我的插件
    public function my_list($user_id, $args = [])
    {
        $page = $args['page'] ?? 1;
        $limit = $args['limit'] ?? 10;
        $keyword = $args['keyword'] ?? '';
        $where = [['a.user_id', '=', $user_id]];
        if ($keyword) {
            $where[] = ['b.name', 'like', "%{$keyword}%"];
        }
        $model = new UserPlugins();
        $data = $model->alias('a')
            ->join('plugins b', 'a.plugin_id=b.plugin_id', 'left')
            ->field('a.*,b.name,b.icon,b.remark,b.path')
            ->where($where)
            ->order('a.sort desc,a.id desc')
            ->page($page, $limit)
            ->select();
        $total = $model->alias('a')
            ->join('plugins b', 'a.plugin_id=b.plugin_id', 'left')
            ->where($where)
            ->count();
        return self::success_return('success', [
            'list' => $data,
            'total' => $total
        ]);
    }
}
